package com.xique.park.service.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.xique.common.core.text.Convert;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.client.ClientLaneConfig;
import com.xique.park.mapper.client.ClientLaneConfigMapper;
import com.xique.park.service.service.IClientLaneConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗亭通道中间Service业务层处理
 *
 * @author caogq
 * @date 2021-08-02
 */
@Service
public class ClientLaneConfigServiceImpl implements IClientLaneConfigService {

    @Autowired
    private ClientLaneConfigMapper clientLaneConfigMapper;

    /**
     * 查询岗亭通道中间
     *
     * @param id 岗亭通道中间ID
     * @return 岗亭通道中间
     */
    @Override
    public ClientLaneConfig selectClientLaneConfigById(Long id) {
        return clientLaneConfigMapper.selectClientLaneConfigById(id);
    }

    /**
     * 查询岗亭通道中间列表
     *
     * @param clientLaneConfig 岗亭通道中间
     * @return 岗亭通道中间
     */
    @Override
    public List<ClientLaneConfig> selectClientLaneConfigList(ClientLaneConfig clientLaneConfig) {
        return clientLaneConfigMapper.selectClientLaneConfigList(clientLaneConfig);
    }

    /**
     * 新增岗亭通道中间
     *
     * @param clientLaneConfig 岗亭通道中间
     * @return 结果
     */
    @Override
    public int insertClientLaneConfig(ClientLaneConfig clientLaneConfig) {
        return clientLaneConfigMapper.insertClientLaneConfig(clientLaneConfig);
    }

    /**
     * 修改岗亭通道中间
     *
     * @param clientLaneConfig 岗亭通道中间
     * @return 结果
     */
    @Override
    public int updateClientLaneConfig(ClientLaneConfig clientLaneConfig) {
        return clientLaneConfigMapper.updateClientLaneConfig(clientLaneConfig);
    }

    /**
     * 删除岗亭通道中间对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteClientLaneConfigByIds(String ids) {
        return clientLaneConfigMapper.deleteClientLaneConfigByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除岗亭通道中间信息
     *
     * @param id 岗亭通道中间ID
     * @return 结果
     */
    @Override
    public int deleteClientLaneConfigById(Long id) {
        return clientLaneConfigMapper.deleteClientLaneConfigById(id);
    }

    @Override
    public int updateClientLaneFunction(ClientLaneConfig search) {
        List<ClientLaneConfig> clientLaneConfigs = clientLaneConfigMapper.selectClientLaneConfigList(search);
        if (clientLaneConfigs.isEmpty() || clientLaneConfigs.size() > 1) {
            return 0;
        }
        ClientLaneConfig clientLaneConfig = clientLaneConfigs.get(0);
        String config = clientLaneConfig.getConfig();
        JSONObject jsonObject = JSONObject.parseObject(config);
        if (StringUtils.isNotEmpty(search.getLocalVideoSwitch())) {
            jsonObject.put("localVideoSwitch", search.getLocalVideoSwitch());
        }
        if (StringUtils.isNotEmpty(search.getRemoteVideoSwitch())) {
            jsonObject.put("remoteVideoSwitch", search.getRemoteVideoSwitch());
        }
        if (StringUtils.isNotEmpty(search.getTemPark())) {
            jsonObject.put("temPark", search.getTemPark());
        }
        if (StringUtils.isNotEmpty(search.getVideoSwitch())) {
            jsonObject.put("videoSwitch", search.getVideoSwitch());
        }
        clientLaneConfig.setConfig(jsonObject.toString());
        int i = clientLaneConfigMapper.updateClientLaneConfig(clientLaneConfig);
        return i;
    }

    @Override
    public List<ClientLaneConfig> selectListByClientId(Long clientId) {
        return clientLaneConfigMapper.selectListByClientId(clientId);
    }

}
